/*** Replication do-file for:
Debus, Lattmann & Wagner, 2024. Mode of Candidacy, Electoral Prospects, and the Ideological Deviation of candidacy-seeking Politicians from their Party Leadership on May 20, 2024. Electoral Studies.
*/

use "analysis_data.dta", replace

********************************************************
*****CREATE SCHULTE-CLOOS COMPETITIVE MEASURE***********
********************************************************
********Relational Competitiveness Measure
*max vote share of party that won in constituency
gen ref = max/2
cap drop schulte_comp
gen schulte_comp = 1 if partei == 1 & afd_share >= ref
replace schulte_comp = 1 if partei == 2 & cdu_share >= ref
replace schulte_comp = 1 if partei == 3 & linke_share >= ref
replace schulte_comp = 1 if partei == 4 & fdp_share >= ref
replace schulte_comp = 1 if partei == 5 & grün_share >= ref
replace schulte_comp = 1 if partei == 6 & spd_share >= ref
replace schulte_comp = 0 if schulte_comp == .

*********Absolute Competitiveness Measure
**SCHULTE DIREKT  > 20
cap drop schulte_abs
gen schulte_abs = 1 if partei == 1 & afd_share >= .20
replace schulte_abs = 1 if partei == 2 & cdu_share >= .20
replace schulte_abs = 1 if partei == 3 & linke_share >= .20
replace schulte_abs = 1 if partei == 4 & fdp_share >= .20
replace schulte_abs = 1 if partei == 5 & grün_share >= .20
replace schulte_abs = 1 if partei == 6 & spd_share >= .20
replace schulte_abs = 0 if schulte_abs == .

***Continoues Competitiveness Measure
*Cont. Variable that measures total distance between first and all other parties and throwing it into the model
cap drop schulte_cont
gen schulte_cont = max - afd_share if partei == 1 & max != afd_share 
replace schulte_cont = max - cdu_share if partei == 2 & max != cdu_share 
replace schulte_cont = max - afd_share if partei == 3 & max != afd_share 
replace schulte_cont = max - linke_share if partei == 4 & max != linke_share 
replace schulte_cont = max - grün_share if partei == 5 & max != grün_share 
replace schulte_cont = max - spd_share if partei == 6 & max != spd_share 
bysort wknr: egen mini = min(schulte_cont)
replace schulte_cont = mini if schulte_cont == .



****************************************
********Recoding*********************
*coding only list candidacy
gen list = 1 if list_pos >= 0 & direct == 0
replace list = 0 if list_pos == . & direct == 1
replace list = 0 if direct == 1


*coding list candidacy with dual
cap drop liscan
gen liscan = 1 if list == 1
replace liscan = 1 if list_pos != . & direct == 1
replace liscan = 0 if liscan == .

*Seniority
cap drop newbi
gen newbi = 1 if bund == 0 & landtag == 0
replace newbi= 0 if newbi == .

*Dependent variable: ideological deviation
cap drop tot_dis_lead
***both variables point_ideo_lead_cul and point_ide_lead_eco are the difference of cultural (p1-q1) and economic ideology (p2-q2) between candidate and party leadership 
gen tot_dis_lead = sqrt(point_ideo_lead_cul^2+point_ideo_lead_eco^2)


***************************************
*************Descriptives**************
***************************************
set scheme plotplainblind
************
*Figure 3 (overall)
************
twoway hist tot_dis_lead, ytitle("Frequency") xtitle("Ideological deviation from leadership") freq

***********
*Figure A1 (seperated)
***********
twoway ///
    (histogram tot_dis_lead, freq bin(20) color(red%30)) (histogram point_ideo_lead_cul, freq bin(20) color(blue%30))(histogram point_ideo_lead_eco, freq bin(20) color(darkgrey%30)) , legend(order(1 "Agg. deviation" 2 "2nd dimension" 3 "1st dimension")) xtitle("Ideo. deviation from leadership")
	

***********
*Figure 4
***********
cap drop mean
egen mean = mean(tot_dis_lead), by(partei)
*here I extract only the first two decimal numbers
cap drop short
cap drop meanString
gen meanString = string(mean)
gen short = substr(meanString,1,4)
replace short = substr(short,1,3) if inlist(short,".817", ".843", ".959", ".972")
destring short, gen(meanp)

twoway scatter partei tot_dis_lead || scatter partei meanp, mla(meanp) ms(black) msymbol(Oh) mlabcolor(black) mlabpos(30) mlabsize(3.4)  ylabel(1 "AfD" 2 "CDU/CSU" 3 "Left" 4 "FDP" 5 "Greens" 6 "SPD") xtitle("Ideological deviation from leadership") ytitle("Party") 



*****************************
**********Regressions********
*****************************
****MODEL 1
****HERE ONLY LIST VS. Only DIRECT
reg tot_dis_lead i.list i.gendr i.newbi age_c i.bundland i.gov vote_dif i.partei if direct == 0 | list_pos ==.,rob cluster(partei)
est store a

****MODEL 2
***DUAL VS. ONLY list
reg tot_dis_lead i.liscan i.gendr i.newbi age_c i.bundland i.gov vote_dif unemploy popdens i.partei,rob cluster(partei)
est store b

****MODEL 3
**here only dual candidates & direct
gen placeo = place1
reg tot_dis_lead i.placeo i.newbi i.gendr age_c i.gov i.bundland vote_dif unemploy popdens i.partei if direct == 1, rob cluster(partei)
est store c

***regression table
***Table 1
esttab a b c, keep(1.list 1.liscan 1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei 1.placeo unemploy popdens) order(1.list 1.liscan 1.placeo  1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei unemploy popdens) mtitles("Only List vs. Only direct" "Dual vs. only list" "Dual & only direct") coeflabels(1.list "List only" 1.liscan "Dual Cand" 1.placeo "First Place" 1.gendr "Gender" 1.newbi "Seniority" age_c "Age" 1.gov "Government" vote_dif "Difference vote share" 2.partei "CDU/CSU" 3.partei "Left" 4.partei "FDP" 5.partei "Greens" 6.partei "SPD" unemploy "Unemployment" popdens "Pop. density") se r2 ar2 replace

***coefficient plot
coefplot (a b c), keep(1.placeo 1.list 1.liscan) xline(0) coeflabels(1.list = "Only List" 1.liscan = "Dual Cand." 1.placeo = "First in Pred.")


*******************************
********ROBUSTNESS*************
*******************************

*************************
***Outlier regression****
*************************
**Coding outlier
cap drop no_outlier
summarize tot_dis_lead, detail
gen no_outlier = 1 if inrange(tot_dis_lead, r(p1), r(p99))

****ONLY LIST VS. Only DIRECT
reg tot_dis_lead i.list i.gendr age_c i.bundland i.gov vote_dif i.partei if direct == 0 | list_pos ==. &no_outlier == 1 ,rob cluster(partei)
est store a

***dual vs. only list
reg tot_dis_lead i.liscan i.gendr i.newbi age_c i.bundland i.gov vote_dif unemploy popdens i.partei if no_outlier == 1,rob cluster(partei)
est store b

**here only dual candidates & direct
reg tot_dis_lead i.placeo i.newbi i.gendr age_c i.gov i.bundland vote_dif unemploy popdens i.partei if direct == 1 & no_outlier  == 1, rob cluster(partei)
est store c

*Table A1
esttab a b c, keep(1.list 1.liscan 1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei 1.placeo unemploy popdens) order(1.list 1.liscan 1.placeo  1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei unemploy popdens) mtitles("Only List vs. Only direct" "Dual vs. only list" "Dual & only direct") coeflabels(1.list "List only" 1.liscan "Dual Cand" 1.placeo "First Place" 1.gendr "Gender" 1.newbi "Seniority" age_c "Age" 1.gov "Government" vote_dif "Difference vote share" 2.partei "CDU/CSU" 3.partei "Left" 4.partei "FDP" 5.partei "Greens" 6.partei "SPD" unemploy "Unemployment" popdens "Pop. density") se r2 ar2 replace

****Figure A2
coefplot (a b c), keep(1.placeo 1.list 1.liscan) xline(0) coeflabels(1.list = "Only list" 1.liscan = "Dual candidacy" 1.placeo = "First in prediction")


***********************
****logged deviation***
***********************

**Logging
gen tot_log = log(tot_dis_lead)

*Figure A4
twoway hist tot_log, ytitle("Frequency") xtitle("Ideological deviation from leadership") freq

****ONLY LIST VS. Only DIRECT
reg tot_log i.list i.gendr i.newbi age_c i.bula i.gov vote_dif i.partei if direct == 0 | list_pos ==. ,rob cluster(partei)
est store a

****dual vs. only list
reg tot_log i.liscan i.gendr i.newbi age_c i.bula i.gov vote_dif unemploy popdens i.partei,rob cluster(partei)
est store b

**here only dual candidates & direct
reg tot_log i.placeo i.newbi i.gendr age_c i.gov i.bula vote_dif unemploy popdens i.partei if direct == 1, rob cluster(partei)
est store c

*Table A2
esttab a b c, keep(1.list 1.liscan 1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei 1.placeo unemploy popdens) order(1.list 1.liscan 1.placeo  1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei unemploy popdens) mtitles("Only List vs. Only direct" "Dual vs. only list" "Dual & only direct") coeflabels(1.list "List only" 1.liscan "Dual Cand" 1.placeo "First Place" 1.gendr "Gender" 1.newbi "Seniority" age_c "Age" 1.gov "Government" vote_dif "Difference vote share" 2.partei "CDU/CSU" 3.partei "Left" 4.partei "FDP" 5.partei "Greens" 6.partei "SPD" unemploy "Unemployment" popdens "Pop. density") se r2 ar2 replace

*Figure A3
coefplot (a b c), keep(1.placeo 1.list 1.liscan) xline(0) coeflabels(1.list = "Only list" 1.liscan = "Dual candidacy" 1.placeo = "First in prediction")





****************************************************
******Using alternative Competition measurements****
****************************************************
****Relational Competition
reg tot_dis_lead i.schulte_comp i.newbi i.gendr age_c i.gov i.bundland vote_dif unemploy popdens i.partei if direct == 1, rob cluster(partei)
est store a

***Absolute Competition
reg tot_dis_lead i.schulte_abs i.newbi i.gendr age_c i.gov i.bundland vote_dif unemploy popdens i.partei if direct == 1, rob cluster(partei)
est store b

***Continoues Competition
reg tot_dis_lead schulte_cont i.newbi i.gendr age_c i.gov i.bundland  vote_dif unemploy popdens i.partei if direct == 1, rob cluster(partei)
est store c

*Figure A5
coefplot (a b c), keep(1.schulte_comp 1.schulte_abs schulte_cont) xline(0) coeflabels(1.schulte_comp  = "Relational competition" 1.schulte_abs = "Absolute competition" schulte_cont = "Cont. competition")

*Table A3
esttab a b c, keep(1.schulte_comp 1.schulte_abs schulte_cont 1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei unemploy popdens) order(1.schulte_comp 1.schulte_abs schulte_cont  1.gendr 1.newbi age_c 1.gov vote_dif 2.partei 3.partei 4.partei 5.partei 6.partei unemploy popdens) mtitles("Only List vs. Only direct" "Dual vs. only list" "Dual & only direct") coeflabels(1.list "List only" 1.liscan "Dual Cand" 1.placeo "First Place" 1.gendr "Gender" 1.newbi "Seniority" age_c "Age" 1.gov "Government" vote_dif "Difference vote share" 2.partei "CDU/CSU" 3.partei "Left" 4.partei "FDP" 5.partei "Greens" 6.partei "SPD" unemploy "Unemployment" popdens "Pop. density") se r2 ar2 replace



****Correlation matrix
*Table A4
pwcorr schulte_comp schulte_abs schulte_cont place1




